﻿using MediumApi.Db;
using MediumApi.Result;
using MediumApi.Service;
using MediumApi.Tools;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using static MediumApi.Param.AppParam;
using static MediumApi.Result.AppResult;

namespace MediumApi.Controllers
{
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class ChannelController : BaseController
    {
        private readonly AppDbContext _context;
        private readonly ConsoleService _consoleService;
        public ChannelController(AppDbContext context, ConsoleService consoleService)
        {
            _context = context;
            _consoleService = consoleService;
        }
        /// <summary>
        /// 查询频道列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<SelectChannelListResult>))]
        public async Task<OperateResult> SelectChannelList(SelectChannelListParam Param)
        {
            return Success(await _consoleService.SelectChannelList(Param));
        }
        /// <summary>
        /// 添加频道
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> AddChannel(AddChannelParam param)
        {
            if (param.Id == 0)
            {
                if (await _consoleService.AddChannel(param) > 0) return Success();
            }
            else
            {
                await _consoleService.UpdateChannel(param); return Success();
            }
            return Error();
        }
        /// <summary>
        /// 删除频道
        /// </summary>
        /// <param name="ChannelId">频道ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<OperateResult> DelChannel(int ChannelId)
        {
            if (await _consoleService.DelChannel(ChannelId) > 0) return Success();
            return Error();
        }
        /// <summary>
        /// 修改频道排序
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> UpdateHomesort(UpdateHomesortParam param)
        {
            if (await _consoleService.UpdateHomesort(param) > 0) return Success();
            return Error();
        }

        /// <summary>
        /// 添加帖子
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ApiAuthority] 
        public async Task<OperateResult> AddContents(AddContentsParam param)
        {
            param.AuthorId = this.UserID;
            if (string.IsNullOrEmpty(param.Title)) param.Title = string.Empty;
            if (string.IsNullOrEmpty(param.Summary)) param.Summary = string.Empty;
            if (string.IsNullOrEmpty(param.Id))
            {
                if (await _consoleService.AddContents(param) > 0) return Success();
            }
            else
            {
                if (await _consoleService.UpdateContents(param) > 0) return Success();
            }

            return Success();
        }
        /// <summary>
        /// 删除帖子
        /// </summary>
        /// <param name="ContentsId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<OperateResult> DelContents(string ContentsId)
        {
            if (await _consoleService.DelContents(ContentsId) > 0) return Success();
            return Error();
        }
        ///<summary>
        /// 查询帖子列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [ProducesResponseType(StatusCodes.Status200OK, Type = typeof(List<SelectContentsListResult>))]
        public async Task<OperateResult> SelectContentsList(SelectContentsListParam param)
        {
            return Success(await _consoleService.SelectContentsList(param));
        }

        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="ContentsId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<OperateResult> Examine(string ContentsId)
        {
            if (await _consoleService.Examine(ContentsId) > 0) return Success();
            return Error();
        }
        /// <summary>
        /// 上下/下架
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> IsDisplay(IsDisplayParam param)
        {
            if (await _consoleService.IsDisplay(param) > 0) return Success();
            return Error();
        }
        /// <summary>
        /// 推荐
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> IsRecommend(IsRecommendParam param)
        {
            if (await _consoleService.IsRecommend(param) > 0) return Success();
            return Error();
        }


        /// <summary>
        /// 添加模板
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> AddTemplate(AddTemplateParam param)
        {
            if (param.Id <= 0)
            {
                if (await _consoleService.AddTemplate(param) > 0) return Success();
            }
            else
            {
                if (await _consoleService.UpdateTemplate(param) > 0) return Success();
            }

            return Success();
        }
        /// <summary>
        /// 删除模板
        /// </summary>
        /// <param name="ContentsId"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<OperateResult> DelTemplate(int Id)
        {
            if (await _consoleService.DelTemplate(Id) > 0) return Success();
            return Error();
        }
        ///<summary>
        /// 查询模板列表
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<OperateResult> SelectTemplateList(SelectTemplateListParam param)
        {
            return Success(await _consoleService.SelectTemplateList(param));
        }
        ///<summary>
        /// 查询模板
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<OperateResult> SelectTemplate(int Id)
        {
            return Success(await _consoleService.SelectTemplate(Id));
        }
    }
}
